#!/bin/sh
#name:Sound control
#desc:Sound control
#package:oddistro
#type:local

# Variables for first time

CFGFILE=/etc/opendomo/sound.conf
VOLUME="0,10,20,30,40,50,60,70,80,90,100"
CARDNUM=0
MIXER=Master
VOL=`amixer sget $MIXER | grep -m1 "]" | cut -f1 -d] | cut -f2 -d[ | cut -f1 -d%`

# If you specity parameters, execute.
if ! test -z $2; then 

	if test -z $3; then
		CARDNUM=0
	else
		CARDNUM=$3
	fi

	MIXER=`echo $1 | tr _ " "` 	# Change MIXER for amixer format 

	# Change VOL for 'form' if amixer don't errors
	if 
		amixer --quiet set -c $CARDNUM "$MIXER" $2% unmute 2>/dev/null
	then
		VOL=`amixer sget "$MIXER" | grep -m1 "]" | cut -f1 -d] | cut -f2 -d[ | cut -f1 -d%`
	else
		VOL=0
	fi

	MIXER=$1			#  Change MIXER for opendomo form format
	alsactl -f $CFGFILE store 2>/dev/null
	chown -f admin:admin $CFGFILE

fi

# Find the mixer controls avariable

for u in `amixer | grep "Simple mixer control" | cut -f1 -d, | cut -f2 -d"'" | tr " " _`; do
        if test "$u" != "$MIXER" ; then
                if test -z $CONTROLS; then
                        CONTROLS="$u"
                else
                        CONTROLS="$CONTROLS","$u"
                fi
        fi
done

# Find sound cards avariable

for i in `ls /sys/class/sound/card* | cut -f3 -dd`; do
        if test "$i" ; then
                if test -z $CARDS; then
                        CARDS="$i"
                else
                        CARDS="$CARDS","$i"
                fi
        fi
done

# 'form' for web interface

#echo "#WARN Remember to save the configuration changes to take effect after restarting"

echo "#> Sound control"
VOL=`amixer sget Master | grep -m1 "]" | cut -f1 -d] | cut -f2 -d[ | cut -f1 -d%`
if test -z "$1"; then
	echo "form:`basename $0`"
	echo "	MIXER	Mixer	hidden	Master"
	echo "	VOL	Volume (%)	list[$VOLUME]	$VOL"

	# Only show cards with two or more cards

	if ! test -z `echo $CARDS | grep -m1 1`; then
		echo "	CARDNUM	Card number	list[$CARDS]	$CARDNUM"
	fi
else
	echo "list:`basename $0`	simple"
	echo "# Volume changed"
	echo "actions:"
fi

echo



